/**
 * Created by Administrator on 2015/12/3.
 */
"use strict";
var F = require('../../../common/function');
var order_F = require('./order_common');
var _o2oServiceApi = require('../../../common/o2oServiceApi');
var o2oServiceApi = new _o2oServiceApi;
var _ = require('underscore');
module.exports.coupon_detail = function*(queryObj){
    //验证店长是否绑定了机构，如果绑定了，通过机构id获取机构信息
    let userResult  =  yield o2oServiceApi.post('authority','userDetail',1,{"act":1,"uid":queryObj.uid});
    console.log(userResult);
    if(userResult.status == 400 || userResult['result']['status']==0 || userResult['result']['sid'] == 0){
        return F.returnMsg(400,'无效的操作，请检查机构是否绑定过店长！');
    }
    let sid = userResult['result']['sid'];
    let rid  = userResult['result']['rid'];

    //通过vid获取店长角色id
    let valid_user =0;
    let roleResult = yield o2oServiceApi.post('authority','roleList',1,{"act":1,"vid":queryObj.vid});
    if(roleResult['result'] && roleResult['result'].length>0){
         roleResult['result'] = _.filter(roleResult['result'],function(item){
            return item.name =='店长'&& item.status ==1;
        });
        if(roleResult['result'] && roleResult['result'].length>0){
            if(roleResult['result'][0]['rid'] == rid){
                valid_user =1;
            }
        }
    }
    if(!valid_user){
        return F.returnMsg(400,'店长不存在，请检查店长有效性！');
    }

    let shopResult = yield o2oServiceApi.post('shop','detail',1,{"act":1,"id":sid});
    if(shopResult.status ==400 || _.isEmpty(shopResult['result'] || shopResult['result']['status'] == 0)){
        return F.returnMsg(400,'无效的机构，请检查机构有效性！');
    }

    queryObj.act=1;
    //通过瓷肌券反查订单信息
    let couponResult = yield  o2oServiceApi.post('order','ticketDetail',1,queryObj);
    if(couponResult.status ==400 || couponResult['result']['error']==1 || (couponResult['result']['error']==0 && couponResult['result']['data'].length ==0)){
        return F.returnMsg(400,'瓷肌券输入错误，请检查！',1);
    }

    let orderid = couponResult['result']['data'][0]['order_id'];
    //查找订单详情
    let orderResult = yield  o2oServiceApi.post('order','detail',1,{"act":1,"orderid":orderid});
    if(orderResult.status == 400 || orderResult['result']['error']==1 || _.isEmpty(orderResult['result'])){
        return F.returnMsg(300, orderResult['message']['msg'], 1);
    }
    let vendorid =  orderResult['result']['wendorid'];
    //验证是否为同一个商家
    if(queryObj.vid != vendorid){
        return F.returnMsg(400,'瓷肌券无效，请检查！',1);
    }
    let returnData ={};
    returnData.ticket_code = couponResult['result']['data'][0]['ticket_code'];
    returnData.wendorid  = orderResult['result']['wendorid'];
    returnData.uid = orderResult['result']['uid'];
    returnData.wareid =  orderResult['result']['wareid'];
    returnData.order_code = orderResult['result']['order_code'];
    returnData.orderid = orderResult['result']['orderid'];
    returnData.wareName = orderResult['result']['wareName'];

    let result =[];
    result.push(orderResult['result']);
    orderResult['result']=result;
    yield  order_F.addUserInfo(orderResult);
    returnData.mobile = orderResult['result'][0]['mobile'] || '';
    returnData.shopName = shopResult['result']['name'];
    return F.returnMsg(200, '查找成功', 1, returnData);
}